<?php

namespace App\Kuafu\Order\Jobs;

use App\Kuafu\Order\Enum\OrderMerchantEnum;
use App\Kuafu\Order\Exceptions\OrderException;
use App\Kuafu\Order\Service\OrderMerchantUseService;
use App\Kuafu\Pay\Exceptions\PayException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

/**
 * 商家延迟生成缓存信息
 * Class OrderPayPolling
 * @package App\Kuafu\Order\Jobs
 */
class OrderMerchantDelayCache implements ShouldQueue
{
    /**
     * php artisan queue:work redis --queue={MERCHANT-API-DELAY-CACHE}
     */
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    const QUEUE_NAME = '{MERCHANT-API-DELAY-CACHE}';

    const CACHE_TYPE_INDEX = 1; // 首页
    const CACHE_TYPE_ORDER = 2; // 店内外明细

    private int   $cacheType;
    private array $data;

    /**
     * Create a new job instance.
     *
     */
    public function __construct(int $cacheType, array $data)
    {
        $this->cacheType = $cacheType;
        $this->data      = $data;

        Log::info('OrderMerchantDelayCache In:', [
            'cacheType' => $cacheType,
            'data'      => $data,
        ]);
    }

    /**
     * Execute the job.
     *
     * @return void
     * @throws OrderException|PayException
     */
    public function handle()
    {
        switch ($this->cacheType) {
            case self::CACHE_TYPE_INDEX:

                $shopId         = $this->data['shopId'];
                $showTimeTab    = $this->data['showTimeTab'];
                $showStartAt    = $this->data['showStartAt'];
                $showEndAt      = $this->data['showEndAt'];
                $compareTimeTab = $this->data['compareTimeTab'];
                $compareStartAt = $this->data['compareStartAt'];
                $compareEndAt   = $this->data['compareEndAt'];
                $orderType      = $this->data['orderType'];

                foreach (OrderMerchantEnum::STAT_INDEX_OVERVIEW as $statisticsType) {
                    (new OrderMerchantUseService([$shopId]))
                        ->setShowTimeTab($showTimeTab, $showStartAt, $showEndAt)
                        ->setCompareTimeTab($compareTimeTab, $compareStartAt, $compareEndAt)
                        ->orderCharts($statisticsType, $orderType);
                }

                break;

            case self::CACHE_TYPE_ORDER:

                $shopId      = $this->data['shopId'];
                $showTimeTab = $this->data['showTimeTab'];
                $showStartAt = $this->data['showStartAt'];
                $showEndAt   = $this->data['showEndAt'];
                $orderType   = $this->data['orderType'];

                if ($orderType == OrderMerchantEnum::ORDER_TYPE_IN) {

                    $statisticsTypes = OrderMerchantEnum::STAT_ORDER_IN_OVERVIEW;
                } elseif ($orderType == OrderMerchantEnum::ORDER_TYPE_WAI_MAI) {
                    $statisticsTypes = OrderMerchantEnum::STAT_ORDER_WAI_MAI_OVERVIEW;
                } else {
                    return;
                }

                foreach ($statisticsTypes as $statisticsType) {
                    (new OrderMerchantUseService([$shopId]))
                        ->setShowTimeTab($showTimeTab, $showStartAt, $showEndAt)
                        ->orderCharts($statisticsType, $orderType);
                }

                break;
        }
    }
}
